﻿using CMS.BaseEntity;
using CMS.BusinessLogic;
using CMS.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CMS.UserControl
{
    public partial class CRUDCategory : System.Web.UI.UserControl
    {
        CategoryLogic cLogic = new CategoryLogic();
        SectionLogic sLogic = new SectionLogic();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["UserId"] == null)
                {
                    Response.Redirect(Directory.HomePage);
                }
                else
                {
                    txtUserName.Text = Session["UserName"].ToString();
                    BindCategoryList();
                    BindDropListSection();
                    if (Request.QueryString["action"] == "cedit")
                    {
                        btnUpdate.Visible = true;
                        btnSave.Visible = false;
                        BinDataToEdit();
                    }
                }
            }
        }

        protected void BindCategoryList()
        {
            DataTable dt = cLogic.GetListCategory();
            grvCategory.DataSource = dt;
            grvCategory.DataBind();
        }

        protected void BindDropListSection()
        {
            DataTable dt = sLogic.GetListSection();
            ddlSection.DataSource = dt;
            ddlSection.DataTextField = "SectionName";
            ddlSection.DataValueField = "SectionId";
            ddlSection.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Category c = new Category();
            c.CategoryName = txtName.Text;
            c.SectionId = int.Parse(ddlSection.SelectedValue.ToString());
            c.AuthorId = int.Parse(Session["UserId"].ToString());
            c.Description = txtDescription.Text;
            c.CreateDate = DateTime.Now.ToString();
            if (chkStatus.Checked == true)
                c.Enable = true;
            else
                c.Enable = false;
            if (cLogic.IsInsertedCategory(c))
            {
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "alert", Script.Manipulate_Success, true);
                BindCategoryList();
                EmptyTextBoxes();
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "alert", Script.Manipulate_Fail, true);
            }
        }

        public void EmptyTextBoxes()
        {
            txtName.Text = string.Empty;
            txtDescription.Text = string.Empty;
            txtName.Focus();
        }

        public void BinDataToEdit()
        {
            Category cate = new Category();
            cate.CategoryId = int.Parse(Request.QueryString["cid"].ToString());
            DataTable dt = cLogic.GetCategoryById(cate);
            DataRow dr = dt.Rows[0];
            txtName.Text = dr["CategoryName"].ToString();
            txtDescription.Text = dr["Description"].ToString();
            ddlSection.SelectedValue = dr["SectionId"].ToString();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            Category cate = new Category();
            cate.CategoryId = int.Parse(Request.QueryString["cid"].ToString());
            cate.CategoryName = txtName.Text;
            cate.SectionId = int.Parse(ddlSection.SelectedValue.ToString());
            cate.AuthorId = int.Parse(Session["UserId"].ToString());
            cate.Description = txtDescription.Text;
            cate.LastModify = DateTime.Now.ToString();
            if (chkStatus.Checked == true)
                cate.Enable = true;
            else
                cate.Enable = false;
            if (cLogic.IsUpdatedCategory(cate))
            {
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "alert", Script.Manipulate_Success, true);
                BindCategoryList();
                EmptyTextBoxes();
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "alert", Script.Manipulate_Fail, true);
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            //Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri);
            txtDescription.Text = String.Empty;
            txtName.Text = String.Empty;
            txtName.Focus();
        }

        protected void grvCategory_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Category cate = new Category();
            cate.CategoryId = int.Parse(grvCategory.DataKeys[e.RowIndex].Value.ToString());
            if (cLogic.IsDeletedCategory(cate))
            {
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "alert", Script.Manipulate_Success, true);
                BindCategoryList();
                EmptyTextBoxes();
            }
            else
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "alert", Script.Manipulate_Fail, true);
        }
    }
}